<?
include('../controller/handler.php');
include('../controller/twitwords.php');
include('../scaffold/user.php');

$hd = new Handler();
$tw = new Twitwords();
$hd->openDB();
$base_url = $hd->getBaseURL();
$alphabet = array (
	'A','B','C','D','E','F','G',
	'H','I','J','K','L','M','N',
	'O','P','Q','R','S','T','U',
	'V','W','X','Y','Z'
);
$page = (isset($_GET['page']) && $_GET['page']>1)? $_GET['page'] : 1;
$users_number = $tw->getUsersNumber();
$quantity = $hd->getUsersPerPage();
$offset = ($page-1)*$quantity;
if(isset($_GET['letter']) && preg_match("/[a-zA-Z]/i", $_GET['letter'])) {
	$users = $tw->loadUserByInitial($_GET['letter'], $offset, $quantity);
	$url = sprintf("letter:%s/", $_GET['letter']);
} else {
	$users = $tw->loadAllUsers($offset, $quantity);
	$url = "";
}
$hd->closeDB();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<?
	/****************
	 * HEAD MODULE
	 ***************/
	include('../module/head.inc.php');
	?>

	<title><?echo $hd->getTitle();?> - People</title>
	
</head>

<body>
<?
/****************
 * HEADER MODULE
 ***************/
include('../module/header.inc.php');
?>
<div id="container">
	<?
	/****************
	 * SEARCH MODULE
	 ***************/
	include('../module/search.inc.php');
	?>
	<?
	if(count($users)==0) echo "<h1>No user to show you...</h1>";
	$i = $offset;
	foreach($users as $user) {
		$i++;
		$style = ($i%2==0)? "even" : "odd";
		?>
		<div class="tag <?echo $style;?>">
		<img class="avatar" src="<?echo $user->getImage();?>" alt="Profile image" />
		<a class="username" href="<?echo $base_url;?>user/<?echo $user->getUsername();?>">
			<?echo $user->getUsername();?>
		</a>
		</div>
		<?
	}
	?>
	<div id="alphabet">
	<a href="<?echo $base_url;?>people">ALL</a> ~
	<?foreach($alphabet as $letter) {?>
	<a href="<?echo $base_url;?>people/letter:<?echo $letter;?>"><?echo $letter;?></a>
	<?}?>
	</div>
	
	<div id="credits"><?echo $hd->getCredits();?></div>
</div>
<div id="footer">
	<div id="nav">
		<?if($page>1) {
			?><a href="<?echo $base_url?>people/<?echo $url;?>page:<?echo $page-1;?>">&larr; previous</a><?
		}?>
		<?if(!isset($_GET['letter'])) {
			?> { <?echo $users_number;?> users } <?
		}?>
		<?if($users_number>$offset+$quantity) {
			?><a href="<?echo $base_url?>people/<?echo $url;?>page:<?echo $page+1;?>">next &rarr;</a><?
		}?>
	</div>
	<?
	/****************
	 * FOOTER MODULE
	 ***************/
	include('../module/footer.inc.php');
	?>
</div>
<?include('../module/analytics.inc.php');?>
</body>
</html>